<?php

namespace app\index\controller;
use app\index\model\BaseModel;
use app\admin\model\ConfigModel;
use think\Controller;

class Base extends Controller
{
    public $user_id = 0;
    public $user_info = [];
    public $config = [];
    public $offer_list = [];
    public function _initialize()
    {
//        session('user_id',30);
        if (!session('user_id')) {
//            header('location:'.url('index/Index/notice').'?msg=请重新扫码登录');
            $this->error('请重新扫码登录');
            exit();
        }
        $this->user_id = session('user_id');
        $info = Db('member')->where(['id'=>session('user_id')])->find();
        if(!$info){
            session('user_id',null);
            $this->error('用户信息异常');
//            return json(['code'=>0,'msg'=>'用户信息异常']);
            exit();
        }else{
            if(!$info['status']){
                $this->error('信息审核中');
            }
        }

        if(!strpos($info['head_img'],'ttp')){
            $info['head_img'] = 'http://' . $_SERVER['SERVER_NAME'].'/uploads/face/'.$info['head_img'];
        }

        $this->user_info = $info;
       
        $config = cache('db_config_data');

        $offer_list = Db('member_offer')->field('id,offer_name as name')->select();
        $this->offer_list = $offer_list;

        if(!$config){
            $config = load_config();                          
            cache('db_config_data',$config);
        }
        $this->config = $config;
        config($config); 

        if(config('web_site_close') == 0 && session('user_id') !=1 ){
//            return json(['code'=>0,'msg'=>'站点已经关闭，请稍后访问~']);
            $this->error('站点已经关闭，请稍后访问~');
        }

        if(config('admin_allow_ip') && session('user_id') !=1 ){
            if(in_array(request()->ip(),explode('#',config('admin_allow_ip')))){
//                return json(['code'=>0,'msg'=>'403:禁止访问']);
                $this->error('403:禁止访问');
            }
        }

    }
}